package com.glsc.ngateway.common.base.converter.oaflow;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.util.CollectionUtils;
import com.glsc.ngateway.common.api.common.dto.AppendIdDto;
import com.glsc.ngateway.common.api.oaflow.dto.xirtrade.TOaFlowXirTradeBaseInfoDto;
import com.glsc.ngateway.common.api.oaflow.dto.xirtrade.TOaFlowXirTradeInfoGSBDto;
import com.glsc.ngateway.common.api.oaflow.dto.xirtrade.TOaFlowXirTradeInfoZJYYBDto;
import com.glsc.ngateway.common.base.domain.mysql.gateway.oaflow.xirtrade.TOaFlowXirTradeInfo;
import com.glsc.ngateway.common.base.domainmapper.TOaFlowXirTradeInfoMapper;
import org.apache.logging.log4j.util.Strings;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class TOaFlowXirTradeInfoConverter {
    public static TOaFlowXirTradeBaseInfoDto do2Dto(TOaFlowXirTradeInfo domain) {
        if (Objects.isNull(domain)) {
            return null;
        }
        TOaFlowXirTradeBaseInfoDto dto = TOaFlowXirTradeInfoMapper.INSTANCE.domain2dto(domain);
        dto.setFj(Strings.isNotBlank(domain.getFj()) ? JSONUtil.toBean(domain.getFj(), AppendIdDto.class) : null);
        return dto;
    }

    public static List<TOaFlowXirTradeBaseInfoDto> do2Dto(List<TOaFlowXirTradeInfo> domainList) {
        return CollectionUtils.isEmpty(domainList) ? java.util.Collections.emptyList() : domainList.stream().map(TOaFlowXirTradeInfoConverter::do2Dto).collect(Collectors.toList());
    }

    public static TOaFlowXirTradeInfo dto2Do(TOaFlowXirTradeBaseInfoDto dto) {
        if (Objects.isNull(dto)) {
            return null;
        }
        TOaFlowXirTradeInfo domain = TOaFlowXirTradeInfoMapper.INSTANCE.dto2domain(dto);
        domain.setFj(Objects.nonNull(dto.getFj()) ? JSONUtil.toJsonStr(dto.getFj()) : null);
        return domain;
    }

    public static TOaFlowXirTradeInfo dto2Do(TOaFlowXirTradeInfoGSBDto dto) {
        if (Objects.isNull(dto)) {
            return null;
        }
        TOaFlowXirTradeInfo domain = TOaFlowXirTradeInfoMapper.INSTANCE.dto2domain(dto);
        domain.setFj(Objects.nonNull(dto.getFj()) ? JSONUtil.toJsonStr(dto.getFj()) : null);
        if (CollectionUtil.isNotEmpty(dto.getDetailList())) {
            TOaFlowXirTradeInfoGSBDto.TOaFlowXirTradeInfoDetailGSBDto detail = dto.getDetailList().get(0);
            domain.setChangeAmount(new BigDecimal(detail.getPayAmount().replace(",", "")));
            domain.setSettleBeginDate(detail.getPaymentDate());
            domain.setSettleFeeDesc(dto.getSettleFeeDesc());
        }
        return domain;
    }

    public static TOaFlowXirTradeInfo dto2Do(TOaFlowXirTradeInfoZJYYBDto dto) {
        if (Objects.isNull(dto)) {
            return null;
        }
        TOaFlowXirTradeInfo domain = TOaFlowXirTradeInfoMapper.INSTANCE.dto2domain(dto);
        domain.setFj(Objects.nonNull(dto.getFj()) ? JSONUtil.toJsonStr(dto.getFj()) : null);
        return domain;
    }

    public static <T extends TOaFlowXirTradeBaseInfoDto> List<TOaFlowXirTradeInfo> dto2Do(List<T> dtoList) {
        if (CollectionUtils.isEmpty(dtoList)) {
            return new ArrayList<>(0);
        }
        return dtoList.stream().map(d -> {
            if (d instanceof TOaFlowXirTradeInfoZJYYBDto) {
                return TOaFlowXirTradeInfoConverter.dto2Do((TOaFlowXirTradeInfoZJYYBDto) d);
            } else if (d instanceof TOaFlowXirTradeInfoGSBDto) {
                return TOaFlowXirTradeInfoConverter.dto2Do((TOaFlowXirTradeInfoGSBDto) d);
            } else {
                return TOaFlowXirTradeInfoConverter.dto2Do((TOaFlowXirTradeBaseInfoDto) d);
            }
        }).collect(Collectors.toList());
    }
}
